package com.metaverse.backend.config;

import com.metaverse.backend.security.JwtUser;
import com.metaverse.backend.utils.SecurityUtils;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;

import javax.annotation.Nonnull;
import java.util.Optional;

@Component
public class SpringSecurityAuditorAware implements AuditorAware<String> {

    @Override
    @Nonnull
    public Optional<String> getCurrentAuditor() {
        String auditor = "system";
        JwtUser user = SecurityUtils.getAuthenticatedUser();
        if (user != null) {
            auditor = user.getUsername() + "(" + user.getId() + ")";
        }
        return Optional.of(auditor);
    }
}
